package com.six.service;

import com.six.domain.entity.Dish;
import com.six.mapper.DishMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;   // 新加
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class DishService {

    private final DishMapper dishMapper;

    /** 新增菜品（带门店） */
    @CacheEvict(value = "menu", key = "#dish.storeId") // 清该门店菜单缓存
    public void save(Dish dish) {
        if (dish.getStoreId() == null) {
            throw new IllegalArgumentException("storeId 不能为空");
        }
        dish.setIsActive(1);
        dishMapper.insert(dish);
    }

    /** 修改菜品（按 id + 门店） */
    @CacheEvict(value = "menu", key = "#dish.storeId")
    public void update(Dish dish) {
        if (dish.getId() == null || dish.getStoreId() == null) {
            throw new IllegalArgumentException("id 和 storeId 不能为空");
        }
        dishMapper.updateById(dish);
    }

    /** 批量删除（限本门店） */
    @CacheEvict(value = "menu", key = "#storeId")
    public void deleteBatch(Integer storeId, List<Long> ids) {
        if (storeId == null || ids == null || ids.isEmpty()) {
            throw new IllegalArgumentException("storeId 或 ids 不能为空");
        }
        dishMapper.deleteBatch(storeId, ids);
    }

    /** 模糊查询（限本门店）——只读，不加 @CacheEvict */
    public List<Dish> searchByName(Integer storeId, String keyword) {
        if (storeId == null) {
            throw new IllegalArgumentException("storeId 不能为空");
        }
        if (keyword == null) keyword = "";
        return dishMapper.searchByNameLike(storeId, keyword.trim());
    }
}